<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Http\Repository\CollageRepository;
use App\Models\Collage;
use App\Models\CollageInfo;
use App\Models\CollageMember;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CollageController extends Controller
{
    /**
     * @return false|string
     * Su ang
     * 团购
     */
    public function collage()
    {
        return CollageRepository::collage();
    }

    /**
     * @param Request $request
     * @return array
     * 经纪人团购列表
     */
    public function collageList(Request $request)
    {
        //接收经纪人id
        $intermediary_id = $request->get('id');
        //判断参数
        if (empty($intermediary_id)) {
            return ['code' => 10004, 'msg' => '参数不全', 'data' => []];
        }
        //获取当前经纪人的开团信息
        $collageData = Collage::getCollageData($intermediary_id);
        return ['code' => 10001, 'msg' => '请求成功', 'data' => $collageData];
    }

    /**
     * @param Request $request
     * @return array
     * 团购详情信息
     */
    public function collageDetail(Request $request)
    {
        //接收拼团id
        $collage_id = $request->get('collage_id');
        if (empty($collage_id)) {
            return ['code' => 10004, 'msg' => '参数不全', 'data' => []];
        }
        //查询单条数据
        $detail = Collage::getDetails($collage_id);
        //返回参数
        return ['code' => 10001, 'msg' => '请求成功', 'data' => $detail];
    }

    /**
     * @param Request $request
     * @return array
     * 经纪人开团
     */
    public function startCollage(Request $request)
    {
        //接收参数
        $params = $request->all();
        //参数处理
        unset($params['id']);
        $collage_id = $params['collage_id'];
        //查询是否有开团信息
        $collageInfo = CollageInfo::getDetail($collage_id);
        //有开团信息 表示该经纪人已经开过此团
        if ($collageInfo) {
            return ['code' => 10002, 'msg' => '您已经开过团了', 'data' => []];
        }
        //开启事务
        DB::beginTransaction();
        //异常处理
        try {
            //添加拼团信息
            $addInfoRes = CollageInfo::addInfo($params);
            //修改拼团的状态
            $updateRes = Collage::updateData($collage_id);
            if ($addInfoRes && $updateRes) {
                //提交事务
                DB::commit();
                return ['code' => 10001, 'msg' => '开团成功', 'data' => []];
            }
            //回滚
            DB::rollBack();
            return ['code' => 10004, 'msg' => '开团失败', 'data' => []];
        } catch (\Exception $e) {
            DB::rollBack();
            return ['code' => 10004, 'msg' => '开团失败', 'data' => []];
        }
    }

    /**
     * @param Request $request
     * @return array
     * 修改拼团状态
     */
    public function changeStatus(Request $request)
    {
        //获取拼团id
        $collage_id = $request->get('collage_id');
        //判断参数
        if (empty($collage_id)) {
            return ['code' => 10004, 'msg' => '参数不全', 'data' => []];
        }
        //修改拼团状态
        $changeRes = Collage::changeStatus($collage_id);
        //判断修改结果 返回数据
        if (!$changeRes) {
            return ['code' => 10004, 'msg' => '修改失败', 'data' => []];
        }
        return ['code' => 10001, 'msg' => '修改成功', 'data' => []];
    }

    /**
     * @param Request $request
     * @return array
     * 创建拼团
     */
    public function createCollage(Request $request)
    {
        //获取经纪人id
        $intermediary_id = $request->get('id');
        //接收输入的参数
        $params = $request->all();
        //参数处理
        unset($params['id']);
        $params['intermediary_id'] = $intermediary_id;
        //创建拼团
        $createRes = Collage::createData($params);
        if (!$createRes) {
            return ['code' => 10004, 'msg' => '创建失败', 'data' => []];
        }
        return ['code' => 10001, 'msg' => '创建成功', 'data' => []];
    }

    /**
     * @param Request $request
     * @return array
     */
    public function collageInfo(Request $request)
    {
        //接收参数
        $collage_id = $request->get('collage_id');
        //判断参数
        if (empty($collage_id)) {
            return ['code' => 10004, 'msg' => '参数不全', 'data' => []];
        }
        //获取拼团的信息 以及参加拼团用户的信息
        $memberData = Collage::getCollageInfo($collage_id);
        //返回数据
        return ['code' => 10001, 'msg' => '请求成功', 'data' => $memberData];
    }

    /**
     * @param Request $request
     * @return array
     * 删除拼团用户
     */
    public function deleteUser(Request $request)
    {
        //接收拼团id
        $collage_id = $request->get('collage_id');
        //接收用户id
        $user_id = $request->get('user_id');
        //开启事务
        DB::beginTransaction();
        //异常处理
        try {
            //通过用户id 删除拼团成员表数据
            $deleteRes = CollageMember::deleteData($collage_id, $user_id);
            //修改拼团的人数
            $minusRes = Collage::minusPeopleNum($collage_id);
            //判断结果
            if ($deleteRes && $minusRes) {
                //事务提交
                DB::commit();
                return ['code' => 10001, 'msg' => '删除成功', 'data' => []];
            }
            //事务回滚
            DB::rollBack();
            return ['code' => 10004, 'msg' => '删除失败', 'data' => []];
        } catch (\Exception $e) {
            //抛出异常
            DB::rollBack();
            return ['code' => 10004, 'msg' => $e->getMessage(), 'data' => []];
        }
    }

    /**
     * @param Request $request
     * @return array
     * 经纪人删除拼团
     */
    public function deleteCollage(Request $request)
    {
        //接收拼团id
        $collage_id = $request->get('collage_id');
        //接收经纪人id
        $intermediary_id = $request->get('id');
        //判断参数
        if (!$collage_id || !$intermediary_id) {
            return ['code' => 10004, 'msg' => '参数不全', 'data' => []];
        }
        //查询经纪人是否是该团的开团者
        $where = ['id' => $collage_id, 'intermediary_id' => $intermediary_id];
        $collageInfo = Collage::collageInfo($where);
        //不是该经纪人的拼团 返回错误信息
        if (!$collageInfo) {
            return ['code' => 10004, 'msg' => '您不是该拼团的发布人', 'data' => []];
        }
        //判断该拼团状态 开启状态不能删除
        if ($collageInfo->status == 1) {
            return ['code' => 10004, 'msg' => '请先关闭拼团', 'data' => []];
        }
        //开启事务
        DB::beginTransaction();
        //异常处理
        try {
            //删除拼团信息
            Collage::deleteCollage($collage_id);
            //批量删除参与该拼团的用户
            CollageMember::deleteMember($collage_id);
            //事务提交
            DB::commit();
            return ['code' => 10001, 'msg' => '删除成功', 'data' => []];
        } catch (\Exception $e) {
            //事务回滚 抛出异常
            DB::rollBack();
            return ['code' => 10004, 'msg' => '删除失败', 'error' => $e->getMessage(), 'data' => []];
        }
    }
}
